package com.dmbjz.consumer;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.dmbjz.config.RabbitConfig;
import com.dmbjz.dao.SettlementDao;
import com.dmbjz.entity.dto.OrderMessageDTO;
import com.dmbjz.entity.po.SettlementPO;
import com.dmbjz.enummeration.SettlementStatus;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

import static com.dmbjz.enummeration.OrderStatus.SETTLEMENT_CONFIREED;


/* 结算监听 */
@Component
@Slf4j
public class SettlementConsumer {

    @Autowired
    private SettlementDao settlementDao;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /* 处理订单 */
    @RabbitListener(queues = RabbitConfig.SETTLEMENT_QUEUE,containerFactory = "rabbitListenerContainerFactory")
    public void orderMessage(Message message, Channel channel) throws IOException {

        String bodyStr = new String(message.getBody());
        OrderMessageDTO messageDTO = JSON.parseObject(bodyStr, OrderMessageDTO.class);
        log.info("结算服务获取到订单消息: {}", messageDTO);

        /* 结算落库 */
        String settleId = IdUtil.randomUUID();
        SettlementPO settlementPO = new SettlementPO();
        settlementPO.setId(settleId);
        settlementPO.setAmount(messageDTO.getPrice());
        settlementPO.setDate(LocalDateTime.now());
        settlementPO.setOrderId(messageDTO.getOrderID());
        settlementPO.setStatus(SettlementStatus.SUCCESS);
        settlementPO.setTransactionId(IdUtil.fastUUID());
        settlementDao.insert(settlementPO);


        /* 更新订单状消息数据 */
        messageDTO.setOrderStatus(SETTLEMENT_CONFIREED);
        messageDTO.setSettlementId(settleId);

        /* 发送消息 */
        MessageProperties properties = new MessageProperties();
        properties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
        properties.setContentEncoding("utf-8");
        String jsonMsg = JSON.toJSONString(messageDTO);
        Message sendMsg = new Message(jsonMsg.getBytes(StandardCharsets.UTF_8),properties);

        CorrelationData correlationData = new CorrelationData();
        ReturnedMessage returnedMessage = new ReturnedMessage(sendMsg,300,"餐厅收到订单消息，发送给订单交换机",
                RabbitConfig.SETTLEMENT_EXCHANGE,
                "key.order");
        correlationData.setReturned(returnedMessage);
        correlationData.setId(messageDTO.getOrderID());         //设置订单ID

        rabbitTemplate.convertAndSend(RabbitConfig.SETTLEMENT_EXCHANGE,"key.order",sendMsg,correlationData);


        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);        //手动消息应答

    }



}
